<?php

/**
 * 会员注册登录
 */
namespace User\Controller;

use Common\Controller\MemberbaseController;
use Org\Util\Date;

class QianbaoController extends MemberbaseController {
    //登录
    
	function _initialize(){
		parent::_initialize();
		
	}
	
	
	public function index() {

		{


			if (IS_POST) {


				$price=I('post.price');


				if(!$price){

					$this->error("请选择充值项目!");
				}
//			$price=0.01;

				$order["order_sn"]=date('ymd').substr(time(),-5).substr(microtime(),2,5);

				$order["uid"]=$this->u["id"];

				$order["price_sum"]=$price;

				$order["inputtime"]=time();
				$order["modal"]="chongzhi";

				$order["title"]="会员充值";
				$order["status"]=1;

				$ad_s=D("Order")->add($order);
//			$order["price_sum"]=0.02;
//
				if($ad_s){

					redirect(U("User/Weixin/pay",array("order_sn"=>$order['order_sn'])));
				}else{
					$this->error("订单生成失败");
				}
				//echo M()->getLastSql();
				//edirect(U("Phone/Weixin/pay",array("order_sn"=>$order['order_sn'])));
				//print_r($order);
				//redirect("http://w.weimarket.cn/wap/power/wxpay.html?order_sn=".$order['order_sn']." &sum_price=".$order[sum]."&content=".$title." &token=bvqtyw1437098034");




			}else{


				$this->assign ( "seo_title",  "钱包" );

				$guize=D("CzGz")->order("price asc")->select();


				foreach($guize as $k=>$v){


					if($v["default"]){

						$guize[$k]["class"]="am-btn-danger";


						$guize_ttile=$v["title"];
					}else{
						$guize[$k]["class"]="am-btn-default";

					}


				}


				$this->assign ( "guize_ttile",  $guize_ttile );
				$this->assign ( "guize",  $guize );

				$this->display("");





			}

		}




    }

    //退出



	function log(){


		$map["uid"]=$this->u["id"];

		$count = D("QbLog")->where($map)->count();
		$page = $this->page ( $count, 20);
		$list =D("QbLog")->limit ( $page->firstRow . ',' . $page->listRows )->where($map)->order ( array (

			"id" => "DESC"
		) )->select ();

		$this->assign ( "Page", $page->show () );

		$this->assign("_list",$list);


		$this->assign ( "seo_title",  "交易记录 " );


		$this->display ();


	}
	function jiangpin(){





		$map["uid"]=$this->u["id"];
		$map["product_name"]=array("neq","");
		$map["num"]=array("gt",0);

		$list =D("QbLog")->where($map)->order ( array (

			"id" => "DESC"
		) )->select ();



		$this->assign("_list",$list);


		$this->display("");

	}


	function lingqu(){


		if(IS_POST){

			$uid=$this->u["id"];
			$id=I('post.id');
			$shop_id=I('post.shop_id');
			$name=I('post.name');
			$tel=I('post.tel');
			$address=I('post.address');


			if(!$tel){

				$this->error("请选择收货地址");
			}

			$info=D("QbLog")->find($id);


			if(!$info["cishu"]){

				$this->error("已经领完了");

			}


			if(time()<$info["next_time"]){

				$this->error("本月已经领取了,请下个月再来吧");
			};


			$new_num=$info[cishu]-1;

			$now_time=time();

			$d=array(
				"id"=>$id,
				"cishu"=>$new_num,
				"last_time"=>$now_time
			);

			if($new_num!=0){

				$d["next_time"]=$now_time+30*24*3600;

			}




			D("QbLog")->save($d);





			$time=time();


			$order_sn=date('ymd').substr(time(),-5).substr(microtime(),2,5);


			$order=array(
				"uid"=>$uid,
				"tel"=>$tel,
				"title"=>$info["product_name"],
				"shop_id"=>$shop_id,
				"address"=>$address,
				"name"=>$name,
				"price_sum"=>0,
				"status"=>2,
				"inputtime"=>$time,
				"modal"=>"cz_jiangpin",
				"ip"=>ip2long(get_client_ip()),
				"order_sn"=>$order_sn,
				"gl_id"=>"$id",


			);

			D("Order")->add($order);



			$data=array(
				"order_sn"=>$order_sn,
				"num"=>$info["num"],
				"product_name"=>$info["product_name"],
			);


			D("OrderInfo")->add($data);



			$this->success("领取成功!",U('User/Order/index'));



		}else{



			$id=I('get.id');
			$uid=$this->u["id"];
			$info=D("QbLog")->find($id);



			$address=D("Address")->where(array("uid"=>$uid))->order("'default'  desc")->select();


			if(!$info["cishu"]){

				$this->error("已经领完了");

			}

			if(time()<$info["next_time"]){
				$this->error("本月已经领取了,请下个月再来吧");

			}




			$this->assign("default_address",$address[0]);
			$this->assign("address",$address);
			$this->assign("info",$info);
			$this->display();
		}




	}




}
?>
